package cn.iocoder.yudao.module.infrastructure.region.controller.app;

import cn.hutool.core.lang.tree.Tree;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.location.LocationInfo;
import cn.iocoder.yudao.module.infrastructure.region.service.RegionService;
import cn.iocoder.yudao.module.infrastructure.region.vo.PinyinCityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 行政区域") // FIXME 新增
@RestController
@RequestMapping("/system/region")
@Validated
public class AppRegionController {
    @Resource
    private RegionService regionService;

    @GetMapping("/tree")
    @Operation(summary = "获得省市区县三级树形结构数据")
    @PermitAll
    public CommonResult<List<Tree<Integer>>> getRegionTree() {
        return success(regionService.getRegionTree());
    }

    @GetMapping("/city-list")
    @Operation(summary = "获得城市列表")
    @Parameter(name = "dataset", description = "数据集：default、adapay", example = "default")
    @PermitAll
    public CommonResult<List<PinyinCityVO>> getCityList(@RequestParam(value = "dataset", required = false, defaultValue = "default") String dataset) {
        return success(regionService.getCityList(dataset));
    }

    @GetMapping("/location")
    @Operation(summary = "根据经纬度获得省市区县信息", description = "若需详细地址，请换用`location-detail`接口")
    @Parameter(name = "longitude", description = "经度", required = true, example = "106.71348")
    @Parameter(name = "latitude", description = "纬度", required = true, example = "26.578342")
    @Parameter(name = "provider", description = "默认高德地图及天地图随机一个，提供商：default-GeoJSON、amap-高德地图、tencent-腾讯地图、tianditu-天地图", example = "amap")
    @PermitAll
    public CommonResult<LocationInfo> getLocation(@RequestParam("longitude") Double longitude, @RequestParam("latitude") Double latitude, @RequestParam(value = "provider", required = false) String provider) {
           return success(regionService.getLocation(longitude, latitude, provider));
    }

    @GetMapping("/location-detail")
    @Operation(summary = "根据经纬度获得详细的地址信息")
    @Parameter(name = "longitude", description = "经度", required = true, example = "106.71348")
    @Parameter(name = "latitude", description = "纬度", required = true, example = "26.578342")
    @Parameter(name = "provider", description = "默认高德地图及天地图随机一个，提供商：default-GeoJSON、amap-高德地图、tencent-腾讯地图、tianditu-天地图", example = "amap")
    @PermitAll
    public CommonResult<String> getLocationAddress(@RequestParam("longitude") Double longitude, @RequestParam("latitude") Double latitude, @RequestParam(value = "provider", required = false) String provider) {
        return success(regionService.getLocationAddress(longitude, latitude, provider));
    }

}
